using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DeliciousTest
{
    public partial class Form1 : Form
    {
        private Delicious fun;
        private Login loginForm;
        /// <revisionHistory>
        /// <revision author="Moayad Mardini [mardini]" date="26-August-2006">
        /// Show the login form as a dialog form instead of a normal form
        /// </revision>
        /// </revisionHistory>
        public Form1()
        {
            InitializeComponent();
            fun = new Delicious();
            loginForm = new Login  ();
            loginForm.ShowDialog();
        }

        public void PassCredentials()
        {
            if ((loginForm.password() != "") && (loginForm.username() != ""))
                fun.SetCredentials(loginForm.username(), loginForm.password());
            else
            {
                MessageBox.Show("Please enter your credentials.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                loginForm.Show();
            }
        }

        /// <revisionHistory>
        /// <revision author="Moayad Mardini [mardini]" date="26-August-2006">
        /// Clear the listbox before load the new items
        /// </revision>
        /// <revision author="Moayad Mardini [mardini]" date="28-August-2006">
        /// Show only the items that begin with "MSDNForums." but after remove that string
        /// </revision>
        /// </revisionHistory>
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            PassCredentials();
            XmlDocument doc = fun.GetUserTags();
            XmlNodeList nodeList = doc.SelectNodes("/tags/tag/@tag");
            listBoxMyTags.Items.Clear();
            foreach (XmlNode n in nodeList)
            {
                if (n.Value.Length > 11)
                {
                    if (n.Value.Substring(0, 11) == @"MSDNForums.")
                    {
                        listBoxMyTags.Items.Add(n.Value.Substring(11, n.Value.Length - 11));
                    }
                }
            }
        }

        //this method uses del.icio.us's API to tag a given post with a string in the
        //"MSDNForums" namespace
        private void buttonTagIt_Click(object sender, EventArgs e)
        {
            PassCredentials();

            //make sure that all of the inputs are available...
            if ((this.textBoxThreadID.Text != "") && (this.textBoxDescription.Text != ""))
            {
                if (this.textBoxNewTag.Text == "")
                    fun.TagThread(this.listBoxMyTags.Text, this.textBoxDescription.Text, this.textBoxThreadID.Text);
                else
                    fun.TagThread(this.textBoxNewTag.Text, this.textBoxDescription.Text, this.textBoxThreadID.Text);
            }
            else
                MessageBox.Show("Please make sure all necessary inputs have been entered.");
        }

        private void buttonFindonTag_Click(object sender, EventArgs e)
        {
            XmlDocument doc;
            if (this.textBoxNewTag.Text == "")
                doc = fun.SearchRecentThreads(this.listBoxMyTags.Text);
            else
                doc = fun.SearchRecentThreads(this.textBoxNewTag.Text);
            XmlNodeList nodeList = doc.SelectNodes("/posts/post/@href");
            string s;
            string[] str;
            string d = "/?=&";
            char[] delimiters = d.ToCharArray();
            foreach (XmlNode n in nodeList)
            {
                s = n.Value;
                str = s.Split(delimiters);
                listBoxResults.Items.Add(str[6]);
            }
        }

        private void buttonOpenThread_Click(object sender, EventArgs e)
        {
            string URL = "http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=" + listBoxResults.Text + "&SiteID=1";
            System.Diagnostics.Process.Start(URL);
        }
        /// <revisionHistory>
        /// <revision author="Moayad Mardini [mardini]" date="26-August-2006">
        /// Create the button and this method
        /// </revision>
        /// </revisionHistory>
        private void signInToAnotherAccountButton_Click(object sender, EventArgs e)
        {
            loginForm = new Login();
            loginForm.ShowDialog();
        }

    }
}